﻿using RabbitMQ.Client;
using Server;
using System;
using System.Text;

namespace TopicsProducer
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("********通配符模式生产者*************");
            var msg = "";
            do
            {
                msg = Console.ReadLine(); ;
                Producer.SendQueueMsg(msg);
                Console.WriteLine("******通配符消息已经发送*****");

            } while (msg.ToLower() != "out");
        }
    }

    class Producer
    {
        private static readonly ConnectionFactory rabbitMqFactory = CommonConnectionFactory.rabbitMqFactory;

        public static void SendQueueMsg(string msg = "0")
        {
            string ExchangeName = "通配符交换机";
            using (IConnection conn = rabbitMqFactory.CreateConnection())
            {
                using (IModel channel = conn.CreateModel())
                {
                    //direct：默认，根据routingKey完全匹配，好处是先匹配再发送
                    //topic：根据绑定关键字通配符规则匹配、比较灵活
                    //Fanout：不需要指定routingkey，相当于群发
                    //header：不太常用，可以自定义匹配规则
                    //声明交换机
                    channel.ExchangeDeclare(ExchangeName, ExchangeType.Topic);

                    //直接发送到交换机，但交换机本身不存储内容，消息数据存在队列中
                    //*一次匹配  #多次匹配
                    //如“audit.#”能够匹配到“audit.irs.corporate”，但是“audit.*” 只会匹配到“audit.irs”

                    var msgBody1 = Encoding.UTF8.GetBytes($@"-error-");
                    channel.BasicPublish(exchange: ExchangeName, routingKey: "info12313131", basicProperties: null, body: msgBody1);

                    var msgBody2 = Encoding.UTF8.GetBytes($@"-error.user-");
                    channel.BasicPublish(exchange: ExchangeName, routingKey: "error.user", basicProperties: null, body: msgBody2);

                    channel.Close();
                }
            }
        }
    }
}

